﻿using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Controllers
{
	#region using
	using System.Web.Mvc;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
	using WebClient.Models.Repositories;
	using WebClient.Helpers;
	using System.Web.Security;
    using WebClient.CF_DAL_WS;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectReply()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_REPLY
			};
			return View(CreateViewModel(data));
		}


		public ActionResult SelectReplyByIDContact(int idcontact)
		{
			var data = new DataTransferViewModel
			{
				IDContact = idcontact,
				EnumViewModelType = EnumViewModel.ADMIN_REPLY,
			};
			return View("SelectReply", CreateViewModel(data));
		}

		public ActionResult SelectReplyByIDContactPaging(int idcontact)
		{
			var data = new DataTransferViewModel
			{
				IDContact = idcontact,
				EnumViewModelType = EnumViewModel.ADMIN_REPLY,
			};
			return PartialView("Templates/TH_ListReply", CreateViewModel(data));
		}


		public ActionResult DeleteReply(int id, int page = 1)
		{
            var idcontact = _repReply.SelectByID(id).IDContact;
			var result = _repReply.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_REPLY,
				CurrentPage = page,
                IDContact = idcontact
			};
			return View("SelectReply", CreateViewModel(data));
		}

		public ActionResult EditReply(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_REPLY,
				CurrentPage = page,
				IntID = id
			};
			return PartialView("Templates/TH_EditReply", CreateViewModel(data));
		}

		public ActionResult CancelEditingReply(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_REPLY,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListReply", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateReply(FormCollection forms)
		{
			string id = forms.GetValues("Reply_ID")[0];
			string idcontact = forms.GetValues("Reply_IDContact")[0];
			string content = forms.GetValues("Reply_Content")[0];
			string userreply = forms.GetValues("Reply_UserReply")[0];
			var reply = _repReply.SelectByID(int.Parse(id));
			reply.IDContact = int.Parse(idcontact.Replace(".", "").Replace(",", ""));
			reply.Content = content;
			reply.UserReply = userreply;
			var result = _repReply.Update(reply);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_REPLY,
			};
			return View("SelectReply", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertReply(FormCollection forms)
		{
			string idcontact = forms.GetValues("Reply_IDContact")[0];
			string content = forms.GetValues("Reply_Content")[0];
			string userreply = forms.GetValues("Reply_UserReply")[0];
            string subject = forms.GetValues("Reply_Subject")[0];
            string detail = forms.GetValues("Reply_Detail")[0];
            string to = forms.GetValues("Reply_Email")[0];

            var informationWeb = DALService.Instance.InformationWeb_SelectAll().FirstOrDefault();

            if (informationWeb != null)
            {
                WebConfiguration.EmailLienHe= informationWeb.Email;
                WebConfiguration.PasswordEmailLienHe = informationWeb.PassEmail;
            }
            else
            {
                var data = new DataTransferViewModel
                {
                    IDContact = int.Parse(idcontact),
                    EnumViewModelType = EnumViewModel.ADMIN_REPLY,
                    InfoText = "The information of webmail is not configurated, cannot reply for user"
                };
                return View("SelectReply", CreateViewModel(data));
            }

            // First sending email
            var emailContent = RenderPartialViewToString("ReplyEmailTemplate");
            emailContent = emailContent.Replace("[Subject]", subject);
            emailContent = emailContent.Replace("[Detail]", detail);
            emailContent = emailContent.Replace("[ReplyContent]", content);
            emailContent = emailContent.Replace("[EmailContact]", WebConfiguration.EmailLienHe);

            var result = SendingMail("Reply letter from - " + WebConfiguration.SiteName, emailContent, to);
            if (result)
            {
                // Then insert to db
                var newItem = new ReplyDTO
                {
                    IDContact = int.Parse(idcontact),
                    Content = content,
                    UserReply = userreply
                };
                result = _repReply.Insert(newItem);
            }
            
			var data2 = new DataTransferViewModel
			{
				IDContact = int.Parse(idcontact),
				EnumViewModelType = EnumViewModel.ADMIN_REPLY,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectReply", CreateViewModel(data2));
		}
	}
}